package com.chuangjiangx.member.basic.ddd.query;

import com.chuangjiangx.member.basic.ddd.dal.mapper.WXPublicUserInfoDalMapper;
import com.chuangjiangx.member.basic.dao.mapper.InMbrPayGiftCardRuleMapper;
import com.chuangjiangx.member.basic.dao.model.InMbrPayGiftCardRule;
import com.chuangjiangx.member.basic.dao.model.InMbrPayGiftCardRuleExample;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrConfig;
import com.chuangjiangx.member.coupon.ddd.domain.repository.MbrConfigRepository;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrWxCard;
import com.chuangjiangx.member.coupon.ddd.domain.repository.MbrWxCardRepository;
import com.chuangjiangx.member.basic.ddd.query.condition.CanOpenWxPushCondition;
import com.chuangjiangx.member.basic.ddd.query.condition.QueryPayGiftCardRuleConfition;
import com.chuangjiangx.member.basic.ddd.query.dto.MbrPayGiftCardRuleDTO;
import com.chuangjiangx.member.coupon.ddd.domain.model.SwicthEnum;
import com.chuangjiangx.member.coupon.ddd.domain.model.WxStatusEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @Auther: YZJ
 * @Date: 2018/9/3 15:46
 * @Description:支付即会员规则查询
 */
@Component
public class MbrPayGiftCardRuleQuery {

    @Autowired
    private InMbrPayGiftCardRuleMapper inMbrPayGiftCardRuleMapper;
    @Autowired
    private WXPublicUserInfoDalMapper wxPublicUserInfoDalMapper;
    @Autowired
    private MbrConfigRepository mbrConfigRepository;
    @Autowired
    private MbrWxCardRepository mbrWxCardRepository;
    /**
     * 查询商户配置的支付即会员规则
     * @param confition
     * @return
     */
    public MbrPayGiftCardRuleDTO searchRule(QueryPayGiftCardRuleConfition confition){
        InMbrPayGiftCardRuleExample inMbrPayGiftCardRuleExample = new InMbrPayGiftCardRuleExample();
        inMbrPayGiftCardRuleExample.createCriteria().andMerchantIdEqualTo(confition.getMerchantId().getId());
        List<InMbrPayGiftCardRule> list = inMbrPayGiftCardRuleMapper.selectByExample(inMbrPayGiftCardRuleExample);
        MbrPayGiftCardRuleDTO mbrPayGiftCardRuleDTO = new MbrPayGiftCardRuleDTO();
        if (list.isEmpty()){
            return mbrPayGiftCardRuleDTO;
        }
        BeanUtils.copyProperties(list.get(0),mbrPayGiftCardRuleDTO);
        return mbrPayGiftCardRuleDTO;
    }

    /**
     * 判断能否开启微信推送
     * @param canOpenWxPushCondition
     * @return
     */
    public boolean canOpenWxPush(CanOpenWxPushCondition canOpenWxPushCondition){
        MbrWxCard mbrWxCard = mbrWxCardRepository.fromMerchantId(canOpenWxPushCondition.getMerchantId());
        Assert.notNull(mbrWxCard,"商户未配置微信卡信息");
        MbrConfig mbrConfig = mbrConfigRepository.findByMerchantId(canOpenWxPushCondition.getMerchantId());
        Assert.notNull(mbrConfig,"商户未配置会员卡信息");
        if (mbrWxCard.getWxStatus().equals(WxStatusEnum.AUDIT_SUCCESS)&&mbrConfig.getWxSyncSwitch().equals(SwicthEnum.ON)){
            return true;
        }
        return false;
    }
}
